﻿$(document).ready(function() {
    GridDemo.Home.Grid1.setupGrid($("#grid1"), $("#pager1"), $("#search1"));
});

GridDemo.Home.Grid1 = {
    setupGrid: function(grid, pager, search) {
        var lastsel;
        grid.jqGrid({
            url: "TwoGrids/List1",
            mtype: "get",
            datatype: "json",
            colNames: ['ProductID', 'ProductName', 'UnitPrice'],
            colModel: [
                        { name: 'ProductID', index: 'ProductID', width: 50, align: 'center', sortable: false, editable: true, hidden: true },
                        { name: 'ProductName', index: 'ProductName', sortable: true, editable: true, hidden: false },
                        { name: 'UnitPrice', index: 'UnitPrice', width: 40, align: 'center', formatter: 'currency', formatoptions: { prefix: "$" }, sortable: true, editable: true, hidden: false }
                      ],
            rowNum: 10,
            rowList: [10, 20, 50],
            pager: pager,
            sortname: 'ProductID',
            sortorder: "asc",
            viewrecords: true,
            multiselect: false,
            onSelectRow: function(id) {
                if (id && id !== lastsel) {
                    //grid.restoreRow(lastsel);
                    grid.saveRow(lastsel, checksave);
                    grid.editRow(id, true);
                    lastsel = id;
                    // key event
                    $("td input[type='text']:enabled").unbind('keyup');
                    $("td input[type='text']:enabled").keyup(function(event) {
                        if (event.keyCode == 40) {  // mui ten xuong
                            var rowindex = id;
                            grid.saveRow(rowindex.toString());
                            rowindex = parseInt(rowindex, 0) + 1;
                            grid.editRow(rowindex.toString());
                            grid.setSelection(rowindex.toString());
                            //window.event.keyCode = 27;
                        }
                        else if (event.keyCode == 38) { // mui ten len
                            var rowindex = id;
                            grid.saveRow(rowindex.toString());
                            rowindex = parseInt(rowindex, 0) - 1;
                            grid.editRow(rowindex.toString());
                            grid.setSelection(rowindex.toString());
                        }
                    });
                    $("editable:last").unbind('keyup');
                    $("editable:last").keyup(function(event) {
                        var rowindex = id;
                        grid.saveRow(rowindex.toString());
                        rowindex = parseInt(rowindex, 0) + 1;
                        grid.setSelection(rowindex.toString());
                        grid.editRow(rowindex.toString());
                    });
                    // end key event
                    return;
                    // autocomplete
                    $('#').autocomplete('TwoGrids/Lookup', {
                        dataType: 'json',
                        parse: function(data) {
                            var rows = new Array();
                            for (var i = 0; i < data.length; i++) {
                                rows[i] = { data: data[i], value: data[i].ProductName, result: data[i].QuantityPerUnit };
                            }
                            return rows;
                        },
                        formatItem: function(row, i, n) {
                            return row.ProductName + ' - ' + row.QuantityPerUnit;
                        },
                        width: 300,
                        mustMatch: true
                    });
                    // end autocomplete
                }
            },
            editurl: "TwoGrids/Create1",
            width: '100%',
            height: '100%',
            autowidth: true,
            rownumbers: true,
            gridview: true,
            caption: 'Grid1'
        })
        .navGrid(pager, { edit: false, add: false, del: false, search: false, refresh: false });
        search.filterGrid("#" + grid.attr("id"), {
            gridModel: false,
            filterModel: [{
                label: 'Search',
                name: 'search',
                stype: 'text'
}]
            });
        }
    };
    function checksave(result) {
        if (result == "") {
            alert("Update is missing!");
            return false;
        }
        return true;
    }
    